package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.SupBean;
import com.project.mapper.ISupMapper;
import com.project.service.ISupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SupServiceImpl implements ISupService {
@Autowired
private ISupMapper supMapper;

    @Override
    public IPage<SupBean> cutByItem(Integer pageNO, String name, String type) {
        QueryWrapper<SupBean> qw= new QueryWrapper<>();
        if (name != null && name.length()!=0){
            qw.like("s_name",name);
        }
        if (type !=null &&type.length()!=0){
            qw.like("s_type",type);
        }
        return supMapper.selectPage(new Page<>(pageNO,3),qw);
    }

    @Override
    public SupBean findById(Integer id) {

        return supMapper.selectById(id);
    }

    @Override
    public void update(Integer id, String person, String phone) {
        SupBean supBean = supMapper.selectById(id);
        supBean.setPerson(person);
        supBean.setPhone(phone);
        supMapper.updateById(supBean);

    }

    @Override
    public void add(SupBean supBean) {
        supMapper.insert(supBean);

    }

    @Override
    public void addList(List<SupBean> list) {
        for (SupBean supBean:list){
            QueryWrapper<SupBean> qw =new QueryWrapper<>();
            qw.eq("s_name",supBean.getName());
            SupBean sb=supMapper.selectOne(qw);
            if (sb==null){
                supMapper.insert(supBean);
            }
        }

    }

    @Override
    public List<SupBean> findAll() {
        return supMapper.selectList(null);
    }
}
